package com.joysuch.wwyt.api.saferisk;


import com.joysuch.wwyt.bp.service.BpRiskCompanyService;
import com.joysuch.wwyt.core.entity.BaseConfig;
import com.joysuch.wwyt.core.enums.BaseConfigCodes;
import com.joysuch.wwyt.core.service.BaseConfigService;
import lombok.extern.slf4j.Slf4j;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.lang.Nullable;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.TriggerContext;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Service;

import java.util.Date;

@Lazy(false)
@EnableScheduling
@Service
@Slf4j
public class BpSafeRiskJob implements SchedulingConfigurer {


    @Autowired
    private BpRiskCompanyService bpRiskCompanyService;

    private static String cron = "0 00 0 * * ?";

    @Autowired
    private BaseConfigService baseConfigService;

    public  ScheduledTaskRegistrar scheduledTaskRegistrar=new ScheduledTaskRegistrar();


    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(10);
        taskScheduler.initialize();
        taskRegistrar.setTaskScheduler(taskScheduler);
        taskRegistrar.addTriggerTask(new Runnable() {
            @Override
            public void run() {
                sendCompanyPromise();
            }
        }, new Trigger() {
            @Override
            public Date nextExecutionTime(TriggerContext triggerContext) {
                // 任务触发，可修改任务的执行周
                BaseConfig baseConfig = baseConfigService.findFirstConfig(BaseConfigCodes.DEFAULT_TIME_OF_ANNOUNCEMENT);
                if (baseConfig != null && Strings.isNotEmpty(baseConfig.getValue())) {
                    String hour = "00";
                    String minute = "00";
                    String[] strings = baseConfig.getValue().split(":");
                    if (strings[0] != null) {
                        minute = strings[0];
                        hour = strings[1];
                    }
                    String startStr = "0 ";
                    String endStr = " * * ?";
                    String cronStr = startStr + hour + " " + minute + endStr;
                    cron=cronStr;
                }
                CronTrigger trigger = new CronTrigger(cron);
                Date nextExec = trigger.nextExecutionTime(triggerContext);
                scheduledTaskRegistrar=taskRegistrar;
                return nextExec;
            }
        });
    }


    /**
     * 定时任务去获取配置时间
     */
    @Scheduled(cron = "0 0 */1 * * ?")
    public void getCron() {
        String hour = "00";
        String minute = "00";
        BaseConfig baseConfig = baseConfigService.findFirstConfig(BaseConfigCodes.DEFAULT_TIME_OF_ANNOUNCEMENT);
        if (baseConfig != null && Strings.isNotEmpty(baseConfig.getValue())) {
            String[] strings = baseConfig.getValue().split(":");
            if (strings[0] != null) {
                minute = strings[0];
                hour = strings[1];
            }
            String startStr = "0 ";
            String endStr = " * * ?";
            String cronStr = startStr + hour + " " + minute + endStr;
            if (!cron.equals(cronStr)) {
                cron = cronStr;
            }
        }

    }

    /**
     * 更新公告
     */
    public void sendCompanyPromise() {
        bpRiskCompanyService.updateState();

    }


}
